package servlet.recipe.page;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import mysql.JdbcUtil;
import servlet.account.control.InfoCheck;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

@WebServlet("/Style")
public class Style extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<Style_info> styles = new ArrayList<>();
        String sql="select * from recipe_categories";
        JdbcUtil jdbc=new JdbcUtil();

        try (Connection conn =jdbc.connectMySQL()){
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
                Style_info syi= new Style_info();
                syi.setId(rs.getString("category_id"));
                syi.setName(rs.getString("category_name"));
                syi.setImg(rs.getString("category_img"));
                styles.add(syi);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        InfoCheck ifc=new InfoCheck();
        if (ifc.checkCredentials(req)){req.setAttribute("Login","Session");}
        req.setAttribute("styles",styles);
        req.getRequestDispatcher("chushi/jsp/style.jsp").forward(req,resp);
    }
    public static class Style_info{
        private String id;
        private String name;
        private String img;
        public Style_info(){}
        public String getId(){
            return id;
        }
        public String getName(){
            return name;
        }
        public String getImg(){return img;
        }
        public void setId(String id){
            this.id=id;
        }
        public void setName(String name){
            this.name=name;
        }
        public void setImg(String img){
            this.img=img;
        }

    }


}
